package medium最大正方形;

public class Solution {
    public int maximalSquare(char[][] matrix) {
        int row = matrix.length;          //行
        int column = matrix[0].length;    //列

        int n = Math.min(row,column);
        int max = 0;

        for (int k = 1; k <= n; k++) {
            if (k == 1){
                for (int i = k-1; i < row; i++) {              //直接从第二行开始，第二列开始计算
                    for (int j = k-1; j < column; j++) {
                        if((int)matrix[i][j]-'0'>=1){
                            max = k;
                            break;
                        }
                    }
                    if (max == n){
                        return (int)Math.pow(max,2);
                    }
                    if (max == 1){
                        break;
                    }
                }
            }
            else{
                //探究-1 -1 ，-1 0 ，0 -1 . 等坐标如果构成正方形就将 1 1 坐标加一 以此类推
                for (int i = k-1; i < row; i++) {              //直接从第二行开始，第二列开始计算
                    for (int j = k-1; j < column; j++) {
                        int dimension = k-1;
                        if ((int)matrix[i-1][j-1]-'0'>=dimension && (int)matrix[i-1][j]-'0'>=dimension && (int)matrix[i][j-1]-'0'>=dimension && (int)matrix[i][j]-'0'>0){
                            matrix[i][j] = (char)(k+'0');
                            max = k;
                            if (max == n){
                                return (int)Math.pow(max,2);
                            }
                        }
                    }
                }
            }

        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.printf("%c  ",matrix[i][j]);
            }
            System.out.println();
        }
        return (int)Math.pow(max,2);
    }
}
